package com.hm.ibsm.web.mg;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hm.common.util.CommonUtil;
import com.hm.ibsm.auto.helper.SystemSessionHolder;
import com.hm.ibsm.service.AuthService;
import com.hm.ibsm.service.UserService;
import com.hm.ibsm.util.BaseController;
import com.hm.ibsm.vo.user.UserVo;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

/**
 * @author shishun.wang
 * @date 2017年3月9日 上午10:20:34
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
@RequestMapping("/mg/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private AuthService authService;

	private String MODEL_NAME = UserController.class.getName();

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public String userMg() {
		return "mg/user";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public String addUser() {
		try {
			return "mg/user/addUser";
		} catch (Exception e) {
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/detail.do", method = RequestMethod.GET)
	public ModelAndView detailRole(UserVo user) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", userService.getUserDetail(user.getId()));
			return new ModelAndView("mg/user/detailUser", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)
	public ModelAndView updateUser(UserVo user) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", userService.getUserDetail(user.getId()));
			return new ModelAndView("mg/user/updateUser", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/addUser.do", method = RequestMethod.POST)
	public String saveAddUser(UserVo user) {
		try {
			userService.addUser(user);
			return userMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/updateUser.do", method = RequestMethod.POST)
	public String saveUpdateUser(UserVo user) {
		try {
			userService.updateUser(user.getId(), user);
			return userMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/deleteUser.do", method = RequestMethod.GET)
	public String deleteUser(UserVo user) {
		try {
			userService.deleteUser(user.getId());
			return userMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public @ResponseBody String query(UserVo user) {
		try {
			return JSONObject.fromObject(userService.query(SystemSessionHolder.pageParam(), CommonUtil.of(user.getUserName()),
					CommonUtil.of(user.getAccount()), CommonUtil.of(user.getEmail()), CommonUtil.of(user.getMobile()))).toString();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return null;
	}

	@RequestMapping(value = "/settingRoles.do", method = RequestMethod.GET)
	public ModelAndView settingRoles(@RequestParam(name = "user", required = true) String user) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("roles", authService.getUserRoles4CheckBox(
					authService.loadCurrentUserId(SystemSessionHolder.getUserSession().getToken(), SystemSessionHolder.getUserSession().getSecretKey())));
			return new ModelAndView("mg/user/settingRoles", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/saveUserRoles.do", method = RequestMethod.POST)
	public String saveUserRoles(HttpServletRequest request) {
		try {
			String[] roles = request.getParameterValues("roles");
			authService.saveUserRoles(
					authService.loadCurrentUserId(SystemSessionHolder.getUserSession().getToken(), SystemSessionHolder.getUserSession().getSecretKey()),
					roles);
			return userMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}
}
